//
//  HLIMKitVideoChatContentConfig.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitVideoChatContentConfig.h"
#import "HLChatKit.h"

@interface HLIMKitVideoChatContentConfig ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation HLIMKitVideoChatContentConfig


/// 计算气泡尺寸
/// @param cellWidth cell的宽
/// @param message 消息
- (CGSize)contentSize:(CGFloat)cellWidth message:(NIMMessage *)message
{
    NIMRtcCallRecordObject *callRecord = (NIMRtcCallRecordObject *)message.messageObject;
    
    NSString *text = @"";
    
    if (callRecord.callType == NIMRtcCallTypeVideo) {
        switch (callRecord.callStatus) {
            case NIMRtcCallStatusComplete: // 通话结束
            {
                text = @"视频时长 ";
                NSNumber *duration = callRecord.durations.allValues.firstObject;
                if (duration) {
                    text = [text stringByAppendingString:[self showTimeInCall:duration.intValue]];
                }
            }
                break;
            case NIMRtcCallStatusCanceled:  // 通话取消
            {
                text = @"视频已取消";
            }
                break;
            case NIMRtcCallStatusRejected:  // 拒接
            {
                text = @"视频已拒绝";
            }
                break;
            case NIMRtcCallStatusTimeout:   // 超时未接听
            {
                text = @"视频未接通";
            }
                break;
            case NIMRtcCallStatusBusy:  // 对方忙线
            {
                text = @"对方已下线";
            }
                break;
                
            default:
                break;
        }
    } else {
        switch (callRecord.callStatus) {
            case NIMRtcCallStatusComplete: // 通话结束
            {
                text = @"音频时长 ";
                NSNumber *duration = callRecord.durations.allValues.firstObject;
                if (duration) {
                    text = [text stringByAppendingString:[self showTimeInCall:duration.intValue]];
                }
            }
                break;
            case NIMRtcCallStatusCanceled:  // 通话取消
            {
                text = @"音频已取消";
            }
                break;
            case NIMRtcCallStatusRejected:  // 拒接
            {
                text = @"音频已拒绝";
            }
                break;
            case NIMRtcCallStatusTimeout:   // 超时未接听
            {
                text = @"音频未接通";
            }
                break;
            case NIMRtcCallStatusBusy:  // 对方忙线
            {
                text = @"对方已下线";
            }
                break;
                
            default:
                break;
        }
    }
    
    message.text = text;
    
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 23) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
    
    /// 气泡最大160
    /// 根据图片宽度计算图片高度
    
    return CGSizeMake(size.width + 28, 22);
}

/// content view 的名字（一般是类名）
/// @param message 消息
- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLIMKitMessageVideoChatContentView";
}

/// cell内容距离气泡的内间距
/// @param message 消息
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message;
{
    return [[HLChatKit.shareSDK config] setting:message].contentInsets;
}




- (NSString *)showTimeInCall:(int)seconds
{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",(long)seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(long)(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",(long)seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    
    return format_time;
}

@end
